﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier.utils;
using Cashier_Service;
using Cashier_Model;
using System.Collections;
using Cashier_Interface;

namespace Cashier.member
{
    public partial class MemberGradeAddForm : Form
    {
        private MainForm mainform;
        private long id;
        private IReloadData iReloadData;
        private MemberGradeService memberGradeService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="id"></param>
        public MemberGradeAddForm(MainForm mainform, long id, IReloadData iReloadData)
        {
            InitializeComponent();
            this.mainform = mainform;
            this.id = id;
            this.iReloadData = iReloadData;
            this.memberGradeService = new MemberGradeService();

            if (id > 0)
                button_save.Tag = "memberGrade_edit";
            else
                button_save.Tag = "memberGrade_add";
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);

            this.InitialFormData();
        }

        #region 功能方法实现

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        private void InitialFormData()
        {
            try
            {
                if (id > 0)
                {
                    MemberGrade memberGrade = memberGradeService.GetMemberGrade(id, string.Empty);
                    textbox_code.Text = memberGrade.Code;
                    textbox_name.Text = memberGrade.Name;
                    textbox_integralMin.Text = memberGrade.IntegralMin.ToString();
                    textbox_integralMax.Text = memberGrade.IntegralMax.ToString();
                    textbox_discount.Text = memberGrade.Discount.ToString("f2");
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 保存会员等级
        /// </summary>
        private void SaveMemberGrade()
        {
            try
            {
                int depth = 0;
                ArrayList list = new ArrayList();
                FormUtil.GetValidateControlsNotPassed(this, list, depth);
                if (list.Count > 0)
                    return;

                String code = textbox_code.Text.Trim();
                String name = textbox_name.Text.Trim();
                long min = Int64.Parse(textbox_integralMin.Text.Trim());
                long max = Int64.Parse(textbox_integralMax.Text.Trim());
                decimal discount = Decimal.Parse(textbox_discount.Text.Trim());

                MemberGrade memberGrade = null;
                if (this.id > 0)
                    memberGrade = memberGradeService.GetMemberGrade(this.id, string.Empty);
                else
                    memberGrade = new MemberGrade();

                if (memberGrade != null)
                {
                    memberGrade.Code = code;
                    memberGrade.Name = name;
                    memberGrade.IntegralMin = min;
                    memberGrade.IntegralMax = max;
                    memberGrade.Discount = discount;

                    long flag = 0;
                    bool isNew = false;
                    if (memberGrade.Id > 0)
                        flag = memberGradeService.UpdateMemberGrade(memberGrade);
                    else
                    {
                        isNew = true;
                        flag = memberGradeService.InsertMemberGrade(memberGrade);
                    }

                    if (flag > 0)
                    {
                        iReloadData.ReloadDataGridViewData(null);
                        if (isNew)
                        {
                            DialogResult result = MessageBox.Show("保存成功，是否需要继续新增！", "提示", MessageBoxButtons.YesNo);
                            if (result == DialogResult.No)
                            {
                                this.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show("保存成功！");
                            this.Close();
                        }
                    }
                    else
                        MessageBox.Show("保存失败！");
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            this.SaveMemberGrade();
        }

        #endregion

        #region 重写方法

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
            return b;
        }

        #endregion
    }
}
